package com.cocotingo.snail;

import java.awt.Point;
import java.awt.geom.Point2D;

public final class Vector {
	public int x;
	public int y;
	public Vector(int x, int y) {
		this.x = x;
		this.y = y;
	}
	public Vector() {
		this(0, 0);
	}
	public Vector(Vector v) {
		this(v.x, v.y);
	}
	public Vector(Point p) {
		this(p.x, p.y);
	}
	public Vector(Point2D p) {
		this((int) p.getX(), (int) p.getY());
	}
	@Override
	public Object clone() throws CloneNotSupportedException {
		return new Vector(this);
	}
	public Vector add(Vector v) {
		return new Vector(x + v.x, y + v.y);
	}
	public Vector subtract(Vector v) {
		return new Vector(x - v.x, y - v.y);
	}
	public void increase(Vector v) {
		x += v.x;
		y += v.y;
	}
	public void decrease(Vector v) {
		x -= v.x;
		y -= v.y;
	}
	public void reverse() {
		x = -x;
		y = -y;
	}
	public Vector getReversedVector() {
		return new Vector(-x, -y);
	}
	public Vector multiply(float lambda) {
		return new Vector((int) (x * lambda), (int) (y * lambda));
	}
	public void multipliedBy(float lambda) {
		x = (int) (x * lambda);
		y = (int) (y * lambda);
	}
	@Override
	public int hashCode() {
		return y << 16 + x;
	}
	@Override
	public boolean equals(Object obj) {
		if (obj == null) return false;
		else if (obj instanceof Vector) {
			return x == ((Vector) obj).x && y == ((Vector) obj).y;
		} else return false;
	}
	@Override
	public String toString() {
		return String.format("%s[x=%d, y=%d]", getClass().getName(), x, y);
	}
	public Point toPoint() {
		return new Point(x, y);
	}
	public Point2D toPoint2DFloat() {
		return new Point2D.Float(x, y);
	}
	public Point2D toPoint2D() {
		return new Point2D.Double(x, y);
	}
}
